<?php
/**
 * Created by PhpStorm.
 * User: jswei
 * Date: 2018/4/24
 * Time: 8:54
 */

namespace app\admin\model;

use think\Db;
use think\Exception;
use think\Model;

class Mch extends Model{
    protected $pk = 'id';
    protected $autoWriteTimestamp = true;

    /**
     * @param array $where
     * @param int $pageSize
     * @param string $field
     * @return \think\Paginator
     * @throws \think\exception\DbException
     */
    public function getList($where= [], $pageSize=10,$field=''){

        $field = $field ? $field: "{$this->getTable()}.*,think_member.open_id,think_member.nickname";

        $list = $this
            ->field($field)
            ->join('think_member',"think_member.id = {$this->getTable()}.uid")
            ->order('id desc')
            ->where($where)
            ->paginate($pageSize);
        return $list;
    }

    /**
     * @param $mc_id
     * @param $field
     * @return array|\PDOStatement|string|Model|null
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getMchOpenId($mc_id,$field=''){
        $field = $field ? $field: "{$this->getTable()}.*,think_member.open_id,think_member.nickname";
        $list = $this
            ->field($field)
            ->join('think_member',"think_member.id = {$this->getTable()}.uid")
            ->where("think_mch.id",'eq',$mc_id)
            ->find();
        return $list['open_id'];
    }

    /**
     * 消费
     * @param $aid
     * @param $mc_id
     * @param $money
     * @param int $type
     * @param string $out
     * @param string $name
     * @return bool
     */
    public function consumer($aid,$mc_id,$money,$type=1,&$out='',$name=''){
        $info = $this->find($mc_id);
        if(!$info){
            $out = '商户不存在';
            return false;
        }
        if($money>$info['money']){
            $out = '账户余额不足请充值';
            return false;
        }
        $_name = '';
        if($type==1 || $type==3){ //发布活动|生成二维码
            $_name = $type==1?'发布活动': '生成二维码';
            $info->money = $info['money'] - $money;
        }elseif($type==2){      //活动红包
            $_name = '活动红包';
            $info->money = $info['money'] - $money;
            $info->dong_money = $info['dong_money'] + $money;
        }
        $name = $name?$name:$_name;
        Db::startTrans();
        try{
            $info = $info->save();
            $log = new MoneyLog();
            $result = $log->addNew($name,$mc_id,$aid,$money,"-{$money}",$type);
            if($info && $result){
                Db::commit();
                return true;
            }else{
                Db::rollback();
                return false;
            }
        }catch (\Exception $ex){
            Db::rollback();
            return false;
        }
    }

    /**
     * 发布红包
     * @param $name
     * @param $aid
     * @param $mc_id
     * @param $total
     * @param $dong
     * @param string $out
     * @return bool
     */
    public function hbconsumer($name,$aid,$mc_id,$total,$dong,&$out=''){
        $info = $this->find($mc_id);
        if(!$info){
            $out = '商户不存在';
            return false;
        }
        if($total>$info['hb_money']){
            $out = '红包账户余额不足请充值';
            return false;
        }
        $info->hb_money = $info['hb_money'] - $total;
        $info->dong_money = $info['dong_money'] + $dong;
        Db::startTrans();
        try{
            $info = $info->save();
            $log = new MoneyLog();
            $result = $log->addNew($name,$mc_id,$aid,$total,"-{$total}",2);
            if($info && $result){
                Db::commit();
                return true;
            }else{
                Db::rollback();
                return false;
            }
        }catch (\Exception $ex){
            Db::rollback();
            return false;
        }
    }

    /**
     * 解冻账户余额记录
     * @param $name
     * @param $aid
     * @param $mc_id
     * @param $money
     * @param string $out
     * @return bool
     */
    public function dec_consumer($name,$aid,$mc_id,$money,&$out=''){
        $info = $this->find($mc_id);
        if(!$info){
            $out = '商户不存在';
            return false;
        }
        if($money>$info['dong_money']){
            return false;
        }
        Db::startTrans();
        $info->money = $info['money'] + $money;
        $info->dong_money = $info['dong_money'] - $money;
        $_result = $info->save();
        $log = new MoneyLog();
        $result = $log->addNew($name,$mc_id,$aid,$money,"+{$money}",2);
        if($_result && $result){
            Db::commit();
            return true;
        }else{
            Db::rollback();
            return false;
        }
    }

    /**
     * 解冻红包
     * @param $name
     * @param $aid
     * @param $mc_id
     * @param $money
     * @param string $out
     * @return bool
     */
    public function dec_hbconsumer($name,$aid,$mc_id,$money,&$out=''){
        $info = $this->find($mc_id);
        if(!$info){
            $out = '商户不存在';
            return false;
        }
        if($money>$info['dong_money']){
            return false;
        }
        Db::startTrans();
        $info->hb_money = $info['hb_money'] + $money;
        $info->dong_money = $info['dong_money'] - $money;
        $_result = $info->save();
        $log = new MoneyLog();
        $result = $log->addNew($name,$mc_id,$aid,$money,"+{$money}",2);
        if($_result && $result){
            Db::commit();
            return true;
        }else{
            Db::rollback();
            return false;
        }
    }

    /**
     * 是否vip
     * @param int $mc_id
     * @return bool
     */
    public function is_vip($mc_id=0){
        $info = $this->find($mc_id);
        if(!$info){
            return false;
        }
        return $info['is_vip']?true:false;
    }

    /**
     * 获取open_id
     * @param int $id
     * @return string
     */
    public function getOpenIdByMid($id=0){
        $info = $this->get($id);
        if(!$info){
            return '';
        }
        $member = (new Member())->get($info['uid']);
        if($member){
            return $member['open_id'];
        }
        return '';
    }

    /**
     * 获取发布价格
     * @param $mc_id
     * @param $money
     * @param string $out
     * @return bool|float|int
     */
    public function getPublishRealMoney($mc_id,$money,&$out=''){
        $info = $this->find($mc_id);
        if($info['is_vip']){
            $_money = $money['used'] * (1-$money['percent']);
        }else{
            $_money = $money['used'];
        }
        if($info['money']==0 || $info['money']<$_money){
            $out = '账户余额不足请充值';
            return false;
        }
        return $_money;
    }

    /**
     * 格式时间
     * @param $value
     * @return false|string
     */
    protected function getCrossTimeAttr($value){
        return  $value?date('Y-m-d H:i:s',$value):'--';
    }

}